'''
Created on 19/04/2010

@author: Mar
'''
import time

class CPU:
    
    def __init__(self, rQueue, aResourceManager):
        self.isFree = True
        self.readyQueue = rQueue
        self.resourceManager = aResourceManager
    
    def getFreedom(self):
        return self.isFree
    
    def setFreedom(self, x):
        self.isFree = x
        
    def executePCB(self, aPCB):
        self.setFreedom(False)
        while (aPCB.getPC() < len(aPCB.program.instructions)): 
            inst = aPCB.getCurrentInstruction()
            if (inst.isCPU()): 
                self.executeInst(inst)
                aPCB.advance()
            else:
                self.sendToIO(aPCB)
                break
        if (len(aPCB.program.instructions) == aPCB.getPC()):
            aPCB.toFinished() 
        self.setFreedom(True)
        
    def executeInst(self,anInst):
        print(anInst.getWord())
        time.sleep(anInst.getTime())
        
    def sendToPrinter(self, aPCB):
        self.queuePrinter.put(aPCB)
        
    def killPCB(self, aPCB):
        self.readyQueue.get()
        
    def sendToScreen(self, aPCB):
        self.screenQueue.put(aPCB)
    
    def sendToKeyBoard(self, aPCB):
        self.keyBoardQueue.put(aPCB)
      
    def sendToIO(self, aPCB):
        self.resourceManager.distribute(aPCB)
        